package org.aplombh.java.interview.newcoder;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class HJ68_成绩排序 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int flag = in.nextInt();
        Student[] students = new Student[n];
        for (int i = 0; i < n; i++) {
            String name = in.next();
            int score = in.nextInt();
            students[i] = new Student(name, score);
        }

        if (flag == 1) {
            Arrays.sort(students, new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    return o1.score - o2.score;
                }
            });
        } else {
            Arrays.sort(students, new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    return o2.score - o1.score;
                }
            });
        }
        for (Student student : students) {
            System.out.println(student);
        }
    }
}

class Student {
    String name;
    int score;

    @Override
    public String toString() {
        return name + " " + score;
    }

    Student() {
    }

    Student(String name, int score) {
        this.name = name;
        this.score = score;
    }
}